package com.whfc.safety.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.safety.dao.SafetyAreaMapper;
import com.whfc.safety.dto.SafetyAreaDTO;
import com.whfc.safety.entity.SafetyArea;
import com.whfc.safety.param.SafetyAreaAddParam;
import com.whfc.safety.param.SafetyAreaEditParam;
import com.whfc.safety.service.SafetyAreaService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 责任区域
 * @Author hw
 * @Date 2021-08-16 17:05
 * @Version 1.0
 */
@DubboService(interfaceClass = SafetyAreaService.class, version = "1.0.0")
public class SafetyAreaServiceImpl implements SafetyAreaService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SafetyAreaMapper safetyAreaMapper;

    @Override
    public List<SafetyAreaDTO> list(Integer deptId, String name, String keyword) throws BizException {
        logger.info("查询责任区域列表，deptId:{},name:{},keyword:{}", deptId, name, keyword);
        List<SafetyAreaDTO> list = safetyAreaMapper.selectByDeptId(deptId, name, keyword);
        return getTree(list);
    }

    @Override
    public void add(SafetyAreaAddParam param) throws BizException {
        logger.info("添加责任区域，param:{}", param);
        SafetyArea area = safetyAreaMapper.selectByDeptIdAndName(param.getDeptId(), param.getName());
        if (area != null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "责任区域名称重复");
        }
        area = new SafetyArea();
        BeanUtils.copyProperties(param, area);
        safetyAreaMapper.insertSelective(area);
    }

    @Override
    public void edit(SafetyAreaEditParam param) throws BizException {

        SafetyArea area = safetyAreaMapper.selectByPrimaryKey(param.getAreaId());
        if (area == null) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "编辑数据不存在");
        }
        logger.info("编辑责任区域，param：{}", param);
        SafetyArea area1 = safetyAreaMapper.selectByDeptIdAndName(param.getDeptId(), param.getName());
        if (area1 != null && !area.getId().equals(area1.getId())) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "责任区域名称重复");
        }
        BeanUtils.copyProperties(param, area);
        safetyAreaMapper.updateByPrimaryKeySelective(area);
    }

    @Override
    public void del(Integer areaId) throws BizException {
        logger.info("删除责任区域，areaId：{}", areaId);

        Integer num = safetyAreaMapper.selectNumByPid(areaId);
        if (num > 0) {
            throw new BizException(ResultEnum.FAILURE.getCode(), "存在子节点无法删除");
        }
        safetyAreaMapper.del(areaId);
    }


    private List<SafetyAreaDTO> getTree(List<SafetyAreaDTO> list) {
        Map<Integer, SafetyAreaDTO> map = new HashMap<>();
        for (SafetyAreaDTO area : list) {
            if (area.getPid() == null) {
                //为目录根节点
                map.put(area.getAreaId(), area);
                continue;
            }
            if (map.containsKey(area.getPid())) {
                SafetyAreaDTO areaDTO = map.get(area.getPid());
                List<SafetyAreaDTO> children = areaDTO.getChildren();
                if (children == null) {
                    children = new ArrayList<>();
                }
                children.add(area);
                areaDTO.setChildren(children);
                map.put(area.getPid(), areaDTO);
                continue;
            }
            map.put(area.getAreaId(), area);
        }
        return new ArrayList<>(map.values());
    }
}
